package com.wyp168.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wyp168.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wyp168.gulimall.product.entity.AttrEntity;
import com.wyp168.gulimall.product.service.AttrAttrgroupRelationService;
import com.wyp168.gulimall.product.service.AttrService;
import com.wyp168.gulimall.product.service.CategoryService;
import com.wyp168.gulimall.product.vo.AttrGroupRelationVO;
import com.wyp168.gulimall.product.vo.resp.AttrGroupAttrRelationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.wyp168.gulimall.product.entity.AttrGroupEntity;
import com.wyp168.gulimall.product.service.AttrGroupService;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.R;



/**
 * 属性分组
 *
 * @author wyp168
 * @email 2721255240@qq.com
 * @date 2022-10-22 12:09:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    //225/withattr?t=1667979957142
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //1.查出当前分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupAttrRelationVO> vos = attrGroupEntities.stream().map(entity -> {
            AttrGroupAttrRelationVO vo = new AttrGroupAttrRelationVO();
            BeanUtils.copyProperties(entity, vo);
            //2.查出每个属性分组的所有属性
            List<AttrAttrgroupRelationEntity> attrAttrGroupEntities = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", entity.getAttrGroupId()));
            if (!CollectionUtils.isEmpty(attrAttrGroupEntities)) {
                List<Long> attrIds = attrAttrGroupEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                List<AttrEntity> attrEntities = attrService.listByIds(attrIds);
                vo.setAttrs(attrEntities);
            }
            return vo;
        }).collect(Collectors.toList());
        return R.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{cateLogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, String> params,
                  @PathVariable("cateLogId") Long cateLogId){
        PageInfo<AttrGroupEntity> page = attrGroupService.queryPage(params, cateLogId);

        return R.ok().put("page", page);
    }

    //http://localhost:88/api/product/attrgroup/2/attr/relation?t=1667580305522
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }

    //http://localhost:88/api/product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        attrGroupService.deleteRelation(vos);
        return R.ok();
    }

    //http://localhost:88/api/product/attrgroup/8/noattr/relation?t=1667582744936&page=1&limit=10&key=
    @GetMapping("/{arrtGroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("arrtGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    //http://localhost:88/api/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOS) {
        relationService.saveBatchAttrRelation(attrGroupRelationVOS);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
